package com.wh.minihelp.user.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.wh.db.jdbc.mysql.IJDBCRowMapper;
import com.wh.db.jdbc.mysql.IJDBCRowSetter;
import com.wh.db.sql.SQLUtils;
import com.wh.minihelp.flower.domain.FamilyFlower;
import com.wh.minihelp.user.domain.AccountInfo;
import com.wh.minihelp.user.domain.ChildInfo;
import com.wh.minihelp.user.domain.FamilyAccount;
import com.wh.minihelp.user.domain.FamilyInfo;

/**
 * 用于数据操作的元数据
 * 
 * @author washheart@163.com
 */
public class UserDBMetas {
	public static final String//
	TN_AccountInfo = "wh_flower_accountinfo"// 用户信息
			, TN_FamilyInfo = "wh_flower_familyinfo"// 家庭信息
			, TN_ChildInfo = "wh_flower_childinfo"// 小孩信息
			, TN_FamilyAccount = "wh_flower_familyaccount"// 家庭与帐号的关联
			, TN_FamilyChild = "wh_flower_familychild";// 家庭与小孩的关联

	// 读取家庭信息的类
	public static class FamilyInfoMapper implements IJDBCRowMapper<FamilyInfo> {
		static String sql_all = "select id,t_name from " + TN_FamilyInfo;//

		public FamilyInfo mapRow(ResultSet rs, int rowNum) throws SQLException {
			FamilyInfo o = new FamilyInfo();
			o.setId(rs.getString(1));
			o.setName(rs.getString(2));
			return o;
		}
	}

	// 保存家庭信息的类
	public static class FamilyInfoSetter implements IJDBCRowSetter<FamilyInfo> {
		static String SQL_INSERT = "insert into " + TN_FamilyInfo + "(id,t_name)values(?,?)";//

		public void setValues(PreparedStatement ps, FamilyInfo o) throws SQLException {
			ps.setString(1, o.getId());
			ps.setString(2, o.getName());
		}
	}

	// 保存小孩信息的类
	public static class ChildInfoSetter implements IJDBCRowSetter<ChildInfo> {
		static String SQL_INSERT = "insert into " + TN_ChildInfo //
				+ "(id,t_name,t_createDate,t_remark,t_familyId,t_photo,t_age,t_sex,t_totals,t_used,t_recordCount)values(?,?,?,?,?,?,?,?,?,?,?)";//

		public void setValues(PreparedStatement ps, ChildInfo o) throws SQLException {
			ps.setString(1, o.getId());
			ps.setString(2, o.getName());
			ps.setString(3, SQLUtils.toSQLDate(o.getCreateDate()));
			ps.setString(4, o.getRemark());
			ps.setString(5, o.getFamilyId());
			ps.setString(6, o.getPhoto());
			ps.setInt(7, o.getAge());
			ps.setInt(8, o.getSex());
			ps.setInt(9, o.getTotals());
			ps.setInt(10, o.getUsed());
			ps.setInt(11, o.getRecordCount());
		}
	}

	// 修改小孩名称setter
	public static class ChildNameUpdateSetter implements IJDBCRowSetter<ChildInfo> {
		static String SQL_UPDATE_NAME = "update " + TN_ChildInfo + " set t_name=? where id=?";//

		public void setValues(PreparedStatement ps, ChildInfo o) throws SQLException {
			ps.setString(2, o.getId());
			ps.setString(1, o.getName());
		}
	}

	// 修改小孩红花数量的setter
	public static class ChildFlowerNumberUpdateSetter implements IJDBCRowSetter<ChildInfo> {
		public static String SQL_UPDATE_NAME = "update " + TN_ChildInfo + " set t_totals=?,t_used=?, t_recordCount=(t_recordCount+1) where id=?";//

		public void setValues(PreparedStatement ps, ChildInfo o) throws SQLException {
			ps.setInt(1, o.getTotals());
			ps.setInt(2, o.getUsed());
			ps.setString(3, o.getId());
		}
	}

	public static class FamilyChildMapper implements IJDBCRowMapper<ChildInfo> {
		public static String sql_all = "select id,t_name,t_photo,t_totals,t_used,t_remark,t_age,t_sex,t_createDate,t_familyId,t_recordCount"//
				+ " from " + TN_ChildInfo //
		;

		public ChildInfo mapRow(ResultSet rs, int rowNum) throws SQLException {
			ChildInfo child = new ChildInfo();
			child.setId(rs.getString(1));
			child.setName(rs.getString(2));
			child.setPhoto(rs.getString(3));
			child.setTotals(rs.getInt(4));
			child.setUsed(rs.getInt(5));
			child.setRemark(rs.getString(6));
			child.setAge(rs.getInt(7));
			child.setSex(rs.getInt(8));
			child.setCreateDate(rs.getDate(9));
			child.setFamilyId(rs.getString(10));
			child.setRecordCount(rs.getInt(11));
			return child;
		}
	}

	// // 保存小孩信息
	// public static class ChildInfoMapper implements IJDBCRowMapper<ChildInfo> {
	// public static String sql_all = "select id,t_name,t_photo,t_totals,t_used,t_remark,t_age,t_sex,t_createDate,t_recordCount from " + TN_ChildInfo;//
	//
	// public ChildInfo mapRow(ResultSet rs, int rowNum) throws SQLException {
	// ChildInfo o = new ChildInfo();
	// o.setId(rs.getString(1));
	// o.setName(rs.getString(2));
	// o.setPhoto(rs.getString(3));
	// o.setTotals(rs.getInt(4));
	// o.setUsed(rs.getInt(5));
	// o.setRemark(rs.getString(6));
	// o.setAge(rs.getInt(7));
	// o.setSex(rs.getInt(8));
	// o.setCreateDate(rs.getDate(9));
	// o.setRecordCount(rs.getInt(10));
	// return o;
	// }
	// }

	// 读取用户与帐号的关联以及家庭、帐号信息
	public static class FamilyAccountAllMapper implements IJDBCRowMapper<FamilyAccount> {
		static String sql_all = "select fa.t_familyId,fa.t_accountId,fa.t_createDate,fa.t_type,fa.t_isDefault" //
				+ ",f.t_name,a.t_name,a.t_createDate,a.t_remark,t_pwd"//
				+ " from " + TN_FamilyAccount + " fa," + TN_FamilyInfo + " f," + TN_AccountInfo + " a"//
				+ " where fa.t_familyId=f.id and fa.t_accountId=a.id";//

		public FamilyAccount mapRow(ResultSet rs, int rowNum) throws SQLException {
			FamilyAccount fa = new FamilyAccount();
			fa.setFamilyId(rs.getString(1));
			fa.setAccountId(rs.getString(2));
			fa.setCreateDate(rs.getDate(3));
			fa.setType(rs.getInt(4));
			fa.setDefault(rs.getBoolean(5));
			//
			FamilyInfo f = new FamilyInfo();
			f.setId(fa.getFamilyId());
			f.setName(rs.getString(6));
			fa.setFamily(f);
			//
			AccountInfo a = new AccountInfo();
			a.setId(fa.getAccountId());
			a.setName(rs.getString(7));
			a.setCreateDate(rs.getDate(8));
			a.setRemark(rs.getString(9));
			a.setPwd(rs.getString(10));
			fa.setAccount(a);
			return fa;
		}
	}

	// 读取用户信息的类
	public static class AccountInfoMapper implements IJDBCRowMapper<AccountInfo> {
		static String sql_all = "select id,t_name,t_createDate,t_remark,t_pwd from " + TN_AccountInfo;//

		public AccountInfo mapRow(ResultSet rs, int rowNum) throws SQLException {
			AccountInfo a = new AccountInfo();
			a.setId(rs.getString(1));
			a.setName(rs.getString(2));
			a.setCreateDate(rs.getDate(3));
			a.setRemark(rs.getString(4));
			a.setPwd(rs.getString(5));
			return a;
		}
	}

	// 保存用户与帐号的关联
	public static class FamilyAccountSetter implements IJDBCRowSetter<FamilyAccount> {
		static String SQL_INSERT = "insert into " + TN_FamilyAccount //
				+ "(t_familyId,t_accountId,t_createDate,t_type,t_isDefault)values(?,?,?,?,?)";//

		public void setValues(PreparedStatement ps, FamilyAccount o) throws SQLException {
			ps.setString(1, o.getFamilyId());
			ps.setString(2, o.getAccountId());
			ps.setString(3, SQLUtils.toSQLDate(o.getCreateDate()));
			ps.setInt(4, o.getType());
			ps.setBoolean(5, o.isDefault());
		}
	}

	// 保存用户信息的类
	public static class AccountInfoSetter implements IJDBCRowSetter<AccountInfo> {
		static String SQL_INSERT = "insert into " + TN_AccountInfo // 4个字段
				+ "(id,t_name,t_createDate,t_remark,t_pwd)values(?,?,?,?,?)";//

		public void setValues(PreparedStatement ps, AccountInfo o) throws SQLException {
			ps.setString(1, o.getId());
			ps.setString(2, o.getName());
			ps.setString(3, SQLUtils.toSQLDate(o.getCreateDate()));
			ps.setString(4, o.getRemark());
			ps.setString(5, o.getPwd());
		}
	}

	// 更新用户信息的类
	public static class AccountInfoUpdater implements IJDBCRowSetter<AccountInfo> {
		static String SQL_UPDATE = "update " + TN_AccountInfo // 4个字段
				+ " set t_name=?,t_remark=?,t_pwd=? where id=?";//

		public void setValues(PreparedStatement ps, AccountInfo o) throws SQLException {
			ps.setString(1, o.getName());
			ps.setString(2, o.getRemark());
			ps.setString(3, o.getPwd());
			ps.setString(4, o.getId());
		}
	}
}